<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\util\Callout;
use app\util\File;
use app\admin\model\VisitLogs;
use app\admin\model\Custom;
use app\admin\model\Order;
use app\admin\model\SurveyTopic;
use app\admin\model\Facecg;
use app\admin\model\Comment;

class RunController extends CommonController {
    /*
     * 活跃用户
     * author xfwed 
     * date 2020.08.14
     */

    public function indexVisitList() {
        $type = input('param.type');
        $phone = input('param.phone');
        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($type) {//0全部 1打开首页  2打开i美优优
            $where['v.type'] = $type;
        }

        if ($phone) {
            if ($phone == '111111') {
                $where['v.ctm_id'] = 0;
            } else {
                $where['c.custom_phone'] = $phone;
            }
        }

        $where = isset($where) ? $where : [];
        $list = VisitLogs::alias('v')
                ->join('custom c', 'c.id = v.ctm_id', 'left')
                ->where($where)
                ->order('v.id desc')
                ->field('v.id,v.type,v.ctm_id,v.add_time,v.num,c.custom_phone,c.custom_name,c.custom_photo')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 新注册用户
     * author xfwed 
     * date 2020.08.24
     */

    public function newCtmList() {
        $datetype = input('param.datetype');
        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($datetype) {
            $where['add_time'] = ['between', [$datetype . ' 00:00:00', $datetype . ' 23:59:59']];
        }

        $where = isset($where) ? $where : [];
        $where['smallshop_type'] = 0;
        $list = Custom::where($where)
                ->group("DATE_FORMAT(add_time,'%Y%m%d')")
                ->order('add_time desc')
                ->field('group_concat(id) ids,count(id) num,add_time')
                ->paginate($perPage);
        foreach ($list as $k => $v) {
            $arr_str = explode(" ", $v['add_time']);
            $list[$k]['add_time'] = $arr_str[0];
            //新增99会员数
            $list[$k]['specialNum'] = Custom::where('vip_status', 2)->where('id', 'in', $v['ids'])->count();
            $ids = Custom::where('vip_status', 2)->where('id', 'in', $v['ids'])->column('id');
            //新增会员购买项目人数
            $list[$k]['buyItemNum'] = Order::where('ctm_id', 'in', $ids)
                    ->where('type', 'in', '1,2,5')
                    ->where('status', 2)
                    ->group('ctm_id')
                    ->count();
            //新增会员购买商品人数
            $list[$k]['buyGoodsNum'] = Db::table('pay_order')->where('ctm_id', 'in', $ids)
                    ->where('status', '>', 1)
                    ->group('ctm_id')
                    ->count();
            //新增会员开通小店人数
            $list[$k]['smallShopNum'] = Custom::where('is_smallshop', 1)->where('id', 'in', $v['ids'])->count();
        }


        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 问卷调查题目列表
     * author xfwed 
     * date 2021.01.11
     */

    public function surveyTopicList() {
        $type = input('param.type'); //1评价前问卷调查	
        $title = input('param.title'); //标题
        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($type) {
            $where['type'] = $type;
        }

        if ($title) {
            $where['title'] = ['like', '%' . $title . '%'];
        }
        $where = isset($where) ? $where : [];
        $list = SurveyTopic::where($where)
                ->order('id desc')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 添加问卷调查题目
     * author xfwed 
     * date 2021.01.12
     */

    public function addSurveyTopic() {
        $rule = [
            'type' => 'require',
            'title' => 'require',
            'des' => 'require',
        ];
        $msg = [
            'type.require' => '类型必须',
            'title.require' => '标题必须',
            'des.require' => '题目描述必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $data['type'] = $all['type'];
        $data['title'] = $all['title'];
        $data['des'] = $all['des'];
        $data['status'] = 1;
        $data['add_time'] = date('Y-m-d H:i:s');
        $add = SurveyTopic::insert($data);
        if ($add) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 启用/禁用问卷调查题目
     * author xfwed 
     * date 2021.01.12
     */

    public function saveSurveyTopic() {
        $rule = [
            'id' => 'require',
            'status' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
            'status.require' => '状态必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $status = $all['status'];
        $save = SurveyTopic::where('id', $id)->update(['status' => $status]);
        if ($save) {
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }

    /*
     * 人脸融合模板列表
     * author xfwed 
     * date 2021.01.22
     */

    public function facecgList() {
        $type = input('param.type'); //1新年模板	
        $status = input('param.status'); //0全部1是2否删除	
        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($type) {
            $where['type'] = $type;
        }

        if ($status) {
            $where['status'] = $status;
        }

        $where = isset($where) ? $where : [];
        $list = Facecg::where($where)
                ->order('use_num desc,id desc')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 添加人脸融合模板
     * author xfwed 
     * date 2021.01.22
     */

    public function addfacecg() {
        $rule = [
            'type' => 'require',
            'img' => 'require',
            'distanceTop' => 'require',
        ];
        $msg = [
            'type.require' => '类型必须',
            'img.require' => '标题必须',
            'distanceTop.require' => '距离必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $data['type'] = $all['type'];
        $data['status'] = 2;
        $data['img'] = $all['img'];
        $data['distanceTop'] = $all['distanceTop'];
        $data['add_time'] = date('Y-m-d H:i:s');
        $add = Facecg::insert($data);
        if ($add) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }

    /*
     * 删除人脸融合模板
     * author xfwed 
     * date 2021.01.22
     */

    public function delfacecg() {
        $rule = [
            'id' => 'require',
            'status' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
            'status.require' => '状态必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $status = $all['status']; //1是2否删除	
        $save = Facecg::where('id', $id)->update(['status' => $status]);
        if ($save) {
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }

    /*
     * 评价管理
     * author xfwed 
     * date 2021.04.19
     */

    public function commentList() {
        $custom_name = input('param.custom_name'); //用户昵称
        $custom_phone = input('param.custom_phone'); //用户手机号
        $item_name = input('param.item_name'); //项目名称
        $staff_name = input('param.staff_name'); //美容师姓名
        $store_name = input('param.store_name'); //店铺名称
        $ord_num = input('param.ord_num'); //订单号
        $is_best = input('param.is_best'); //是否精华帖 1.是 2.否
        $is_del = input('param.is_del'); //1隐藏2显示
        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        $store_type = input('param.store_type');//店铺类型 0全部 1品牌连锁店 2服务授权店	
        $staff_type = input('param.staff_type');//美容师类型 0全部 1品牌连锁店 2服务授权店

        if ($custom_name) {
            $where['ct.custom_name'] = ['like', '%' . $custom_name . '%'];
            $where1['ct.custom_name'] = ['like', '%' . $custom_name . '%'];
        }
        if ($custom_phone) {
            $where['ct.custom_phone'] = $custom_phone;
            $where1['ct.custom_phone'] = $custom_phone;
        }
        if ($item_name) {
            $where['i.item_name'] = ['like', '%' . $item_name . '%'];
            $where1['i.item_name'] = ['like', '%' . $item_name . '%'];
        }
        if ($staff_name) {
            $where['s.staff_name'] = ['like', '%' . $staff_name . '%'];
            $where1['s.staff_name'] = ['like', '%' . $staff_name . '%'];
        }
        if ($store_name) {
            $where['st.store_name'] = ['like', '%' . $store_name . '%'];
            $where1['st.store_name'] = ['like', '%' . $store_name . '%'];
        }
        if ($ord_num) {
            $where['o.ord_num'] = $ord_num;
            $where1['o.ord_num'] = $ord_num;
        }
        if($is_best){
            $where['c.is_best'] = $is_best;
        }
        if ($is_del) {
            $where['c.is_del'] = $is_del;
            $where1['c.is_del'] = $is_del;
        }
        if($store_type){
            $where['st.type'] = $store_type;
            $where1['st.type'] = $store_type;
        }

        if($staff_type){
            $where['s.type'] = $staff_type;
            $where1['s.type'] = $staff_type;
        }

        $where = isset($where) ? $where : [];
        $where1 = isset($where1) ? $where1 : [];

        $list = Comment::alias('c')
                ->join('custom ct', 'ct.id = c.ctm_id', 'left')
                ->join('item i', 'i.id = c.item_id', 'left')
                ->join('staff s', 's.id = c.staff_id', 'left')
                ->join('store st', 'st.id = c.store_id', 'left')
                ->join('order o', 'o.id = c.order_id', 'left')
                ->join('yuyue y', 'y.id = c.yuyue_id', 'left')
                ->where($where)
                ->field('c.id,o.ord_num,ct.custom_name,ct.custom_phone,y.staff_time,c.add_time,i.item_name,c.item_star,c.item_label,s.staff_name,s.type staff_type,c.staff_star,c.staff_label,st.store_name,st.type store_type,c.store_star,c.store_label,c.item_content,c.item_image,c.is_best,c.is_del,c.red_money,c.order_value,c.admin_img')
                ->order('c.order_value desc,c.add_time desc')
                ->paginate($perPage);
        //print_r(Comment::getLastSql());die;
        foreach ($list as $k => $v) {
            $item_label = Db::name('comment_label')->where('id', 'in', $v['item_label'])->column('name');
            $list[$k]['item_label'] = implode(',', $item_label);

            $staff_label = Db::name('comment_label')->where('id', 'in', $v['staff_label'])->column('name');
            $list[$k]['staff_label'] = implode(',', $staff_label);

            $store_label = Db::name('comment_label')->where('id', 'in', $v['store_label'])->column('name');
            $list[$k]['store_label'] = implode(',', $store_label);

            $list[$k]['all_star'] = ($v['item_star'] + $v['staff_star'] + $v['store_star']) / 3;
        }
        $ctm_num = Comment::alias('c')
                ->join('custom ct', 'ct.id = c.ctm_id', 'left')
                ->join('item i', 'i.id = c.item_id', 'left')
                ->join('staff s', 's.id = c.staff_id', 'left')
                ->join('store st', 'st.id = c.store_id', 'left')
                ->join('order o', 'o.id = c.order_id', 'left')
                ->join('yuyue y', 'y.id = c.yuyue_id', 'left')
                ->where($where1)
                ->group('c.ctm_id')
                ->count();
        $comment_num = Comment::alias('c')
                ->join('custom ct', 'ct.id = c.ctm_id', 'left')
                ->join('item i', 'i.id = c.item_id', 'left')
                ->join('staff s', 's.id = c.staff_id', 'left')
                ->join('store st', 'st.id = c.store_id', 'left')
                ->join('order o', 'o.id = c.order_id', 'left')
                ->join('yuyue y', 'y.id = c.yuyue_id', 'left')
                ->where($where1)
                ->count();
        $best_comment_num = Comment::alias('c')
                ->join('custom ct', 'ct.id = c.ctm_id', 'left')
                ->join('item i', 'i.id = c.item_id', 'left')
                ->join('staff s', 's.id = c.staff_id', 'left')
                ->join('store st', 'st.id = c.store_id', 'left')
                ->join('order o', 'o.id = c.order_id', 'left')
                ->join('yuyue y', 'y.id = c.yuyue_id', 'left')
                ->where($where1)
                ->where('c.is_best', 1)
                ->count();
        $red_money_num = Comment::alias('c')
                ->join('custom ct', 'ct.id = c.ctm_id', 'left')
                ->join('item i', 'i.id = c.item_id', 'left')
                ->join('staff s', 's.id = c.staff_id', 'left')
                ->join('store st', 'st.id = c.store_id', 'left')
                ->join('order o', 'o.id = c.order_id', 'left')
                ->join('yuyue y', 'y.id = c.yuyue_id', 'left')
                ->where($where1)
                ->sum('c.red_money');
        $data['list'] = $list;
        $data['ctm_num'] = $ctm_num;
        $data['comment_num'] = $comment_num;
        $data['best_comment_num'] = $best_comment_num;
        $data['red_money_num'] = $red_money_num;

        return $this->ajaxReturn(0, '查询成功', $data);
    }

    /*
     * 显示/隐藏评价
     * author xfwed 
     * date 2021.04.20
     */

    public function delComment() {
        $rule = [
            'id' => 'require',
            'is_del' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
            'is_del.require' => '状态必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $is_del = $all['is_del']; //1是2否删除	
        $save = Comment::where('id', $id)->update(['is_del' => $is_del]);
        if ($save) {
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }

    /*
     * 加入/取消精华
     * author xfwed 
     * date 2021.04.20
     */

    public function bestComment() {
        $rule = [
            'id' => 'require',
            'is_best' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
            'is_best.require' => '状态必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $is_best = $all['is_best']; //是否精华帖 1.是 2.否
        $red_money = $all['red_money'];
        $row = Comment::where('id', $id)->find();
        if ($is_best == 1) {
            if ($row['red_money'] > 0) {
                $save = Comment::where('id', $id)->update(['is_best' => $is_best]);
                $shareResult = TRUE;
                $redResult = TRUE;
            } else {
                Db::startTrans();
                $save = Comment::where('id', $id)->update(['is_best' => $is_best, 'red_money' => $red_money]);
                $shareResult = Custom::where('id', '=', $row['ctm_id'])->setInc('red_money', $red_money);
                $redData['ctm_id'] = $row['ctm_id'];
                $redData['money'] = $red_money;
                $redData['type'] = 9;
                $redData['add_time'] = date('Y-m-d H:i:s', time());
                $redResult = Db::name('redpacket')->insert($redData);
            }
        } else {
            $save = Comment::where('id', $id)->update(['is_best' => $is_best]);
            $shareResult = TRUE;
            $redResult = TRUE;
        }

        if ($save && $shareResult && $redResult) {
            Db::commit();
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            Db::rollback();
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }
    
    /*
     * 
     * 客户预约调研
     * author xfwed 
     * date 2021.07.19
     */

    public function yuyueSurvey() {
        $name = input('param.name');
        $phone = input('param.phone');
        $is_will = input('param.is_will');//0全部1是2否愿意
        $start_time = input('param.start_time');
        $end_time = input('param.end_time');
        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($name) {
            $where['c.custom_name'] = ['like','%'.$name.'%'];
        }
        if ($phone) {
            $where['c.custom_phone'] = $phone;
        }
        if($is_will){
            $where['y.is_will'] = $is_will;
        }
        if($start_time && $end_time){
            $where['y.add_time'] = ['between',[$start_time,$end_time]];
        }

        $where = isset($where) ? $where : [];
        $list = Db::name('yuyue_survey')
                ->alias('y')
                ->join('custom c', 'c.id = y.ctm_id', 'left')
                ->where($where)
                ->field('y.*,c.custom_phone,c.custom_name')
                ->order('y.id desc')
                ->paginate($perPage);
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 评价设置排序值
     * author xfwed 
     * date 2021.07.21
     */

    public function setCommentOrderValue() {
        $rule = [
            'id' => 'require',
            'order_value' => 'require',
        ];
        $msg = [
            'id.require' => '评价id必须',
            'order_value.require' => '排序值必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $order_value = $all['order_value']; //排序值	
        $save = Comment::where('id', $id)->update(['order_value' => $order_value]);
        if ($save) {
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }
    
    /*
     * 设置评价心服务图片
     * author xfwed 
     * date 2021.07.21
     */

    public function setCommentAdminImg() {
        $rule = [
            'id' => 'require',
            'admin_img' => 'require',
        ];
        $msg = [
            'id.require' => '评价id必须',
            'admin_img.require' => '心服务图片必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $admin_img = $all['admin_img']; //心服务图片
        $save = Comment::where('id', $id)->update(['admin_img' => $admin_img]);
        if ($save) {
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }
    
    /*
     * 评价回复列表
     * author xfwed 
     * date 2021.07.22
     */

    public function commentReplyList() {
        $comment_id = input('param.comment_id');//评价id
        $custom_name = input('param.custom_name'); //评价用户昵称
        $custom_phone = input('param.custom_phone'); //评价用户手机号
        $ctm_name = input('param.ctm_name');//回复用户昵称
        $ctm_phone = input('param.ctm_phone');//回复用户手机
        $item_name = input('param.item_name'); //项目名称
        $staff_name = input('param.staff_name'); //美容师姓名
        $store_name = input('param.store_name'); //店铺名称
        $ord_num = input('param.ord_num'); //订单号
        $is_del = input('param.is_del'); //1显示2隐藏
        $perPage = input('param.perPage');
        $page = input('param.page');
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        $store_type = input('param.store_type');//店铺类型 0全部 1品牌连锁店 2服务授权店	
        $staff_type = input('param.staff_type');//美容师类型 0全部 1品牌连锁店 2服务授权店
        
        if($comment_id){
            $where['cr.comment_id'] = $comment_id;
        }
        if ($custom_name) {
            $where['c.custom_name'] = ['like', '%' . $custom_name . '%'];
        }
        if ($custom_phone) {
            $where['c.custom_phone'] = $custom_phone;
        }
        if ($ctm_name) {
            $where['cr.ctm_name'] = ['like', '%' . $ctm_name . '%'];
        }
        if ($ctm_phone) {
            $where['cr.ctm_phone'] = $ctm_phone;
        }
        if ($item_name) {
            $where['i.item_name'] = ['like', '%' . $item_name . '%'];
        }
        if ($staff_name) {
            $where['s.staff_name'] = ['like', '%' . $staff_name . '%'];
        }
        if ($store_name) {
            $where['st.store_name'] = ['like', '%' . $store_name . '%'];
        }
        if ($ord_num) {
            $where['o.ord_num'] = $ord_num;
        }
        if ($is_del) {
            $where['cr.is_del'] = $is_del;
        }
        if($store_type){
            $where['st.type'] = $store_type;
        }
        if($staff_type){
            $where['s.type'] = $staff_type;
        }

        $where = isset($where) ? $where : [];

        $list = Db::name('comment_reply')->alias('cr')
                ->join('comment co','co.id = cr.comment_id','left')
                ->join('custom c', 'c.id = co.ctm_id', 'left')
                ->join('item i', 'i.id = co.item_id', 'left')
                ->join('staff s', 's.id = co.staff_id', 'left')
                ->join('store st', 'st.id = co.store_id', 'left')
                ->join('order o', 'o.id = co.order_id', 'left')
                ->where($where)
                ->field('cr.id,cr.reply_content,cr.ctm_name,cr.ctm_phone,cr.zan_num,cr.add_time,cr.is_del,c.custom_name,c.custom_phone,i.item_name,s.staff_name,s.type staff_type,st.store_name,st.type store_type,o.ord_num')
                ->order('cr.comment_id desc,cr.add_time desc') 
                ->paginate($perPage);
        
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 显示/隐藏评价回复
     * author xfwed 
     * date 2021.07.22
     */

    public function delCommentReply() {
        $rule = [
            'id' => 'require',
            'is_del' => 'require',
        ];
        $msg = [
            'id.require' => 'id必须',
            'is_del.require' => '状态必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $id = $all['id'];
        $is_del = $all['is_del']; //1.未删除 2.已删除
        $save = Db::name('comment_reply')->where('id', $id)->update(['is_del' => $is_del]);
        if ($save) {
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }

}
